<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编码" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入商品编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="item in statusOptions" :label="item.l" :value="item.v" />
        </el-select>
      </el-form-item>

      <el-form-item label="被判责人" prop="judgeUserName">
        <el-input
          v-model="queryParams.judgeUserName"
          placeholder="请输入被判责人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['other:obligation:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['other:obligation:export']"
        >导出</el-button>
      </el-col>

      <el-col :span="1.5">
        <div style="color: #303133;font-size: 14px;line-height: 30px">
          判责金额：{{priceTotal}} 元
        </div>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="obligationList">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="商品编码" align="center" prop="goodsId" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <el-tag v-for="item in statusOptions" :type="item.t" v-if="scope.row.status === item.v">{{item.l}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否申述" align="center" prop="hasState">
        <template v-slot="scope">
          <el-tag v-if="scope.row.hasState === 1">已申述</el-tag>
          <el-tag v-else type="info">未申述</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="判责金额" align="center" prop="price" />
      <el-table-column label="被判责人" align="center" prop="judgeUserName" />
      <el-table-column label="创建时间" align="center" prop="crateTime" width="180" />
      <el-table-column label="判责原因" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            :icon="scope.row.hasState === 1 ? 'el-icon-view':'el-icon-edit'"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['other:obligation:edit']"
          >{{scope.row.hasState === 1 ? '查看' : '修改'}}</el-button>

          <el-popover
            placement="bottom"
            title="申述原因"
            width="400"
            trigger="manual"
            v-model="scope.row.visible">
            <p>{{scope.row.stateRemark}}</p>
            <div style="text-align: right;margin-top: 5px">
              <el-button type="primary" plain size="mini" @click="updateSubmit(scope.row, 2)">通过</el-button>
              <el-button type="danger" plain size="mini" @click="updateSubmit(scope.row, 1)">驳回</el-button>
            </div>

            <el-button
              type="text" slot="reference" size="mini"
              @click="scope.row.visible = !scope.row.visible"
              icon="el-icon-coordinate"
              style="margin-left: 5px"
              v-hasPermi="['other:obligation:examine']"
              v-if="scope.row.hasState === 1 && scope.row.status === 0"
            >{{scope.row.visible ? '关闭':'审核'}}</el-button>
          </el-popover>

          <el-button
            type="text"
            size="mini"
            v-if="scope.row.status !== 2"
            @click="cancelRow(scope.row)"
            style="margin-left: 5px"
            v-hasPermi="['other:obligation:cancel']"
            icon="el-icon-delete"
          >作废</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <obligation-form :title="title" :open="open" :detail-id="detailId" @close="closeFlush" />
  </div>
</template>

<script>
import { listObligation, exportObligation ,updateObligationExamine, updateObligation, listObligationPrice} from "@/api/other/obligation";
import ObligationForm from "@/views/other/obligation/components/obligation-form.vue";
export default {
  name: "Obligation",
  components: {
    ObligationForm
  },
  data() {
    return {
      priceTotal : 0,
      detailId: undefined,
      //0=判责中，1=已判责，2=作废
      statusOptions: [
        {v: 0, l : '判责中', t: 'warning'},
        {v: 1, l : '已判责', t: 'danger'},
        {v: 2, l : '作废', t: 'info'},
      ],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 判责中心表格数据
      obligationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsId: null,
        goodsName: null,
        status: null,
        hasState: null,
        stateRemark: null,
        price: null,
        judgeUserId: null,
        judgeUserName: null,
        crateTime: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    closeFlush() {
      this.open = false
      this.getList();
    },
    cancelRow(row) {
      this.$confirm("确定作废吗？", "警告", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type:'warning'
      }).then(() => {
        row.status = 2;
        updateObligation(row).then(res => {
          this.msgSuccess("操作成功");
          this.getList();
        })
      })
    },
    updateSubmit(row, type) {
      row.status = type;
      updateObligationExamine(row).then(res => {
        this.msgSuccess("操作成功");
        this.getList();
        this.visible = false;
      })
    },
    /** 查询判责中心列表 */
    getList() {
      this.loading = true;
      listObligation(this.queryParams).then(response => {
        this.obligationList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.obligationList.forEach(it => {
          this.$set(it, "visible", false);
        })
      });

      listObligationPrice(this.queryParams).then(res => {
        this.priceTotal = res.data.price;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "添加判责信息";
      this.detailId = undefined;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      if(row.hasState === 1)
        this.title = "查看判责信息";
      else
        this.title = "修改判责信息";
      this.detailId = row.id;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有判责中心数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportObligation(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
